package com.jesse.eclgy.plug.workflow.service;

import com.jesse.eclgy.plug.util.RequestUtils;
import com.jesse.eclgy.plug.workflow.entity.CustomMap;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;

import java.util.List;

/**
 * Description:
 * Author: jesse
 * Date:2023/7/11 15:07
 */
public class RequestPlugService {

    public void createRequest(CustomMap mainMap, List<List<CustomMap>> tablesDt) throws Exception
    {
        RequestService rs = new RequestService();
        RequestInfo ri = new RequestInfo();
        ri.setCreatorid("129");
        // 创建人
        ri.setWorkflowid("251");
        // 路径ID，指定需要创建的流程
        ri.setDescription("test title");
        // 请求标题
        ri.setRequestlevel("0");
        // 紧急程度，0正常
        ri.setRemindtype("0");
        ri.setIsNextFlow("1");
        // 0 停留在申请人待办，1 提交到下一节点
        ri.setMainTableInfo(RequestUtils.toMainTable(mainMap));
        // 设置主表
        if (!tablesDt.isEmpty())
        {
            ri.setDetailTableInfo(RequestUtils.toDetailTableInfo(tablesDt));
            // 设置明细表
        }

        int requestId = Integer.parseInt(rs.createRequest(ri));
        // 发起流程
        if (requestId < 0)
        {
            throw new RuntimeException(getRequestIdText(requestId));
        }
        System.out.println("发起流程成功，requestId = " + requestId);
    }


    private String getRequestIdText(int requestId)
    {
        if (requestId > 0)
        {
            return "创建流程成功";
        }
        if (requestId == -2)
        {
            return "没有创建权限";
        }
        if (requestId == -4)
        {
            return "保存表单主表数据失败，请检查表单字段类型";
        }
        if (requestId == -5)
        {
            return "更新流程级别失败";
        }
        if (requestId == -6)
        {
            return "无法创建流程待办任务";
        }
        if (requestId == -7)
        {
            return "流程下一节点出错，请检查配置（操作者、出口及条件、附加操作接口等）";
        }
        if (requestId == -8)
        {
            return "流程节点自动赋值操作错误";
        }
        return "未知错误 " + requestId;
    }
}